Ext.define('UM.util.ServerDataUpdater', {});

window.ServerDataUpdater =
{
    GET : 'GET',
    POST : 'POST',

    loadData : function(path, method, callback)
    {
        ServerDataUpdater.jsonCommand('getThemes', {}, method, callback);
    },

    jsonCommand : function(command, params, method, callbacks)
    {
        callbacks = callbacks || {};

        var successFn = function(result)
        {
            var resp = Ext.decode(result.responseText);
            if (!!resp)
            {
                var success = new String(resp.success);
                success = success.toLowerCase();
                if (success == "true" || success == "undefined")
                {
                    if (callbacks.success)
                        callbacks.success(resp);
                } else
                {
                    if (callbacks.failure)
                    {
                        callbacks.failure(resp);
                    }
                    Functions.errorMsg("<b>Error occurred in command '" + command + "':</b><br /><br />" + resp.error, "Command Error");
                }
            } else
            {
                failureFn(result);
            }
        };

        var failureFn = function(response)
        {
            if (callbacks.failure)
            {
                callbacks.failure(
                {
                    success : false,
                    error : response.status + ': ' + response.statusText
                });
            } else
            {
                Functions.errorMsg("<b>Error occurred in command '" + command + "':</b><br /><br />" + response.status + ': ' + response.statusText, "Command Error");
            }
        };
        var url = "/extjs/" + encodeURIComponent(command);
        Ext.Ajax.request(
        {
            url : url,
            //            params : Ext.encode(params),
            method : method,
            success : successFn,
            failure : failureFn
        });
    },

    errorMsg : function(msg, title)
    {
        var message = msg;
        // Check if 'msg' is an error object.
        if (!msg)
        {
            message = "(null message)";
        } else if (msg.message)
        {
            try
            {
                if (!title)
                    title = msg.constructor.name;
            } catch (e)
            {
            }
            message = msg.message;
        }
        title = title || "Error";
        Ext.MessageBox.show(
        {
            title : title,
            msg : message,
            buttons : Ext.MessageBox.OK,
            icon : Ext.MessageBox.ERROR
        });
    }
};